package com.wzdigit.wms.wms.webservice;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.dtos.api.ApiMessage;
import com.wzdigit.wms.basic.domain.transfer.CtnrLoading;
import com.wzdigit.wms.basic.enums.CtnrLoadingStatusEnum;
import com.wzdigit.wms.basic.mapper.core.transfer.CtnrLoadingMapper;
import com.wzdigit.wms.wms.domain.OmsShipBusiness;
import com.wzdigit.wms.wms.domain.dto.OmsShipBusinessDto;
import com.wzdigit.wms.wms.mapper.OmsShipBusinessMapper;
import com.wzdigit.wms.wms.webservice.impl.OmsShipWebService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;
import net.dreamlu.mica.core.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;
@Slf4j
@Component
@WebService(targetNamespace="http://impl.webservice.wms.wms.wzdigit.com",
        endpointInterface = "com.wzdigit.wms.wms.webservice.impl.OmsShipWebService")
public class OmsShipWebserviceImpl implements OmsShipWebService {

    @Autowired
    OmsShipBusinessMapper omsShipBusinessMapper;

    @Autowired
    CtnrLoadingMapper ctnrLoadingMapper;


    @Override
    public ApiMessage saveShipBusiness(OmsShipBusinessDto shipBusiness) {
        //校验必填信息
        //查询新增还是修改
        try {
            log.info("调用装柜信息接口 传入信息==》" + JSON.toJSONString(shipBusiness));
            if(StringUtil.isEmpty(shipBusiness.getOrderNumber())){
                return new ApiMessage(ApiMessage.RESULT_ERROR, "OMS系统发货通知单不能为空");
            }
            /*if(StringUtil.isEmpty(shipBusiness.getOrderLineNumber())){
                return new ApiMessage(ApiMessage.RESULT_ERROR, "OMS系统发货单行号不能为空");
            }*/
            if(StringUtil.isEmpty(shipBusiness.getConsultBoxNumber())){
                return new ApiMessage(ApiMessage.RESULT_ERROR, "OMS参考柜号不能为空");
            }

            QueryWrapper<OmsShipBusiness> queryWrapper = new QueryWrapper<>();
            //queryWrapper.setEntity(new OmsShipBusiness().setOrderNumber(shipBusiness.getOrderNumber()).setOrderLineNumber(shipBusiness.getOrderLineNumber()));
            queryWrapper.setEntity(new OmsShipBusiness().setOrderNumber(shipBusiness.getOrderNumber()).setConsultBoxNumber(shipBusiness.getConsultBoxNumber()));

            OmsShipBusiness record = omsShipBusinessMapper.selectOne(queryWrapper);
            //判断单据有没有关闭
            QueryWrapper<CtnrLoading> query1 = new QueryWrapper<CtnrLoading>();
            query1.setEntity(new CtnrLoading().setGdnNo(shipBusiness.getOrderLineNumber()).setStatus(CtnrLoadingStatusEnum.FINISH.code));//OMS行号对我们单号
            List<CtnrLoading> ctnrLoadings = ctnrLoadingMapper.selectList(query1);
            if (ctnrLoadings != null && ctnrLoadings.size() > 1) {
                return new ApiMessage(ApiMessage.RESULT_ERROR, "装柜单已经完成，不进行更新");
            }
            OmsShipBusiness omsShipBusiness =  new OmsShipBusiness();
            BeanUtil.copyNonNull(shipBusiness,omsShipBusiness);
            //判断插入还是更新
            if (record == null) {
                omsShipBusiness.setCreateTime(new Date());
                omsShipBusiness.setCreateUser("system");
                omsShipBusinessMapper.insert(omsShipBusiness);
                return new ApiMessage(ApiMessage.RESULT_SUCCESS, "新增船务信息成功");
            } else {
                omsShipBusiness.setModifyTime(new Date());
                omsShipBusiness.setModifyUser("system");
                omsShipBusiness.setCreateTime(record.getCreateTime());
                omsShipBusiness.setCreateUser(record.getCreateUser());
                omsShipBusiness.setOmsShipBusinessId(record.getOmsShipBusinessId());
                omsShipBusinessMapper.updateById(omsShipBusiness);
                return new ApiMessage(ApiMessage.RESULT_SUCCESS, "修改船务信息成功");
            }
        }catch (Exception e){
            log.info("调用装柜信息接口异常："+e.getMessage());
            e.printStackTrace();
            return  new ApiMessage(ApiMessage.RESULT_ERROR, "服务器异常");

        }



    }
}
